// Plugin$MxName$.cpp : Defines the exported functions for the DLL application.
//


#include "stdafx.h"
#include "PluginICPopenCV.h"
#include "ImageCuratorPluginLib.h"

void  *MXSTDMETHOD CreateClassInstance(const GUID IId)
{
	void *rc = nullptr;

	if ( MxGUID::IsEqual(IId, IPluginICPopenCVIID::Guid()))
		rc = new PluginICPopenCV();
	else if ( MxGUID::IsEqual(IId, IImageCuratorPluginLibIID::Guid()))
		rc = new ImageCuratorPluginLib();
	else if ( MxGUID::IsEqual(IId, IMxPluginLibDetailsIID::Guid()))
		rc = new PluginICPopenCV();
	else if ( MxGUID::IsEqual(IId, IMxPluginBaseIID::Guid()))
		rc = new PluginICPopenCV();
	else
		rc = nullptr;

	if (rc == nullptr)
	{
		std::string tmp;
		MX_SETERROR(MX11101, MxError::Install, MxError::Abort, MxError::QuietReport, "CreateClassInstance, unsupported IID=%s", MxGUID::ConvertToString(IId, &tmp));
	}
	else
		((IMxPluginBase *)rc)->DuplicatePtr();

	return rc;
}

PluginICPopenCV::PluginICPopenCV(void): 
	_refCnt(0)
{
}

PluginICPopenCV::~PluginICPopenCV(void)
{
}

long MXSTDMETHOD PluginICPopenCV::DestroyPtr(void)
{
	long cnt = InterlockedDecrement(&_refCnt);
	if ( cnt == 0 )
		delete this;
	return cnt;
}

long MXSTDMETHOD PluginICPopenCV::DuplicatePtr(void)
{
	return InterlockedIncrement(&_refCnt);
}

void *MXSTDMETHOD PluginICPopenCV::Dynamic_cast(const GUID IId)
{
	void *rc = nullptr;

	if ( MxGUID::IsEqual(IId, IPluginICPopenCVIID::Guid()))
		rc = static_cast<IPluginICPopenCV *>(this);
	else if ( MxGUID::IsEqual(IId, IMxPluginLibDetailsIID::Guid()))
		rc = static_cast<IMxPluginLibDetails *>(this);
	else if ( MxGUID::IsEqual(IId, IMxPluginBaseIID::Guid()))
		rc = static_cast<IMxPluginBase *>(this);
	else
		rc = nullptr;
	std::string tmp;
	if (rc == nullptr)
		MX_SETERROR(MX11102, MxError::Install, MxError::Abort, MxError::QuietReport, "Dynamic_cast does not support IID=%s", MxGUID::ConvertToString(IId, &tmp));
	else
		((IMxPluginBase *)rc)->DuplicatePtr();

	return rc;
}

int			MXSTDMETHOD PluginICPopenCV::GetInterfaceCount()
{ 
	return 4; 
}

bool		MXSTDMETHOD PluginICPopenCV::IsInterfaceSupported(const GUID iid)
{ 
	bool rc = false;

	if( MxGUID::IsEqual(iid, IPluginICPopenCVIID::Guid()) )
		rc = true;
	else if( MxGUID::IsEqual(iid, IImageCuratorPluginLibIID::Guid()) )
		rc = true;
	else if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
		rc = true;
	else if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
		rc = true;
	else
		rc = false;

	return rc;
}


const GUID	MXSTDMETHOD PluginICPopenCV::GetIID(int index) 
{ 
	GUID rc = GUID_Invalid;
	switch (index)
	{
		case 0:
			 rc = IPluginICPopenCVIID::Guid();
		break;
		case 1:
			rc = IImageCuratorPluginLibIID::Guid();
		break;
		case 2:
			rc = IMxPluginLibDetailsIID::Guid();
		break;
		case 3:
			rc = IMxPluginBaseIID::Guid();
		break;
		default:
		break;
	}
	return rc; 
}

const char *MXSTDMETHOD  PluginICPopenCV::GetISummary( const GUID  iid)
{
	const char *rc = "";

	if( MxGUID::IsEqual(iid, IPluginICPopenCVIID::Guid()) )
		rc = "IPluginICPopenCV - ImageCurator Proc for OpenCV library";
	else if( MxGUID::IsEqual(iid, IImageCuratorPluginLibIID::Guid()) )
		rc = "ImageCuratorPluginLib - provides details about functions for Image Curator";
	else if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
		rc = "IMxPluginLibDetails - provides details about MxPlugin and its interfaces";
	else if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
		rc = "IMxPluginBase - base interface for Plugins (similar to COM IUnknown)";
	else
		rc = "Interface not supported";

	return rc;
}

const char *MXSTDMETHOD  PluginICPopenCV::GetIDetails(const GUID  iid)
{
	const char *rc = "";

	if( MxGUID::IsEqual(iid, IPluginICPopenCVIID::Guid()) )
		rc = "see IPluginICPopenCV.h for details about methods in this interface";
	else if( MxGUID::IsEqual(iid, IImageCuratorPluginLibIID::Guid()) )
		rc = "see IImageCuratorPluginLib.h for details about methods in this interface";
	else if( MxGUID::IsEqual(iid, IMxPluginLibDetailsIID::Guid()) )
		rc = "see IMxPluginLibDetails.h for details about methods in this interface";
	else if( MxGUID::IsEqual(iid, IMxPluginBaseIID::Guid()) )
		rc = "see IMxPluginBase.h for details about methods in this interface";
	else
		rc = "Interface not supported";

	return rc;
}
